<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>修改密码</title>
<link rel="icon" href="favicon.ico" type="image/ico">
<meta name="keywords" content=" ">
<meta name="description" content=" ">
<meta name="author" content="yinqi">
<link href="${pageContext.request.contextPath}/static/css/bootstrap.min.css" rel="stylesheet">
<link href="${pageContext.request.contextPath}/static/css/materialdesignicons.min.css" rel="stylesheet">
<link href="${pageContext.request.contextPath}/static/css/style.min.css" rel="stylesheet">
</head>
  
<body>
<div class="container-fluid p-t-15">
  
  <div class="row">
    <div class="col-lg-12">
      <div class="card">
        <div class="card-body">
          
          <form method="post" class="site-form">
            <div class="form-group">
              <label for="oldpwd">旧密码</label>
              <input type="password" class="form-control" name="oldpwd" id="oldpwd" placeholder="输入账号的原登录密码">
            </div>
            <div class="form-group">
              <label for="newpwd">新密码</label>
              <input type="password" class="form-control" name="newpwd" id="newpwd" placeholder="输入新的密码">
            </div>
            <div class="form-group">
              <label for="repwd">确认新密码</label>
              <input type="password" class="form-control" name="repwd" id="repwd" placeholder="请确认新密码">
            </div>
            <div class="form-group">
              <label for="email">邮箱</label>
              <input type="text" class="form-control" name="email" id="email" value="${user.email}" disabled="disabled" />
            </div>
            <div class="form-group">
              <label for="vcode">输入邮箱验证码</label>
              <div style="float:left;display:flex;width:100%">
                <input type="text" class="form-control" name="vcode" id="vcode" placeholder="请输入邮箱验证码" style="width: 60%">
                <input class="btn btn-info col-lg-2" id="btn" name="btn" value="发送验证码" onclick=" " readonly="readonly" style="width: 30%"/>
              </div>
            </div>
            <input type="hidden" id="ecode" name="ecode" value="ecode"/>
            <button class="btn btn-primary" style="margin-top: 10px" onclick="editpwd()">修改密码</button>
          </form>
 
        </div>
      </div>
    </div>
    
  </div>
  
</div>

<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/bootstrap.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/main.min.js"></script>
<script>
  $(function () {
    $("#btn").click(function () {
      var email=$("#email").val();
      $.ajax({
        type: "post",
        url: "${pageContext.request.contextPath}/user/emailcod",
        data: "email="+email,
        success:function (codes) {
          document.getElementById("btn").value="已发送";
          // $("#btn").attr("value","已发送");
          $("#ecode").attr("value",codes);
        }
      });
    });
  });


  function editpwd() {
    var oldpwd = $("#oldpwd").val();
    var newpwd = $("#newpwd").val();
    var repwd=$("#repwd").val();
    var vcode=$("#vcode").val();
    var ecode=$("#ecode").val();
    // var userData = $("#userForm").serialize();
    if (oldpwd == "") {
      alert("请输入原密码！");
      return false;
    }
    if (newpwd == ""){
      alert("请输入新密码！");
      return false;
    }
    if (newpwd != repwd) {
      alert("两次密码不一致！");
      return false;
    }

    $.ajax({
      url :  "${pageContext.servletContext.contextPath}/user/newpwd",
      data:{"oldpwd":oldpwd,"newpwd":newpwd,"vcode":vcode,"ecode":ecode},
      // data : userData,
      type : "post",
      success (msg) {
        console.log("测试");
        console.log(msg);
        if (msg == 'success') {
          //跳转到登录
          alert("修改成功,请重新登录");
          top.location.href = "${pageContext.servletContext.contextPath}/user/tologin";
        }
        if (msg=='pwdno'){
           alert("原密码有误，请重新修改");
        }
        if (msg=='coderror'){
           alert("验证码有误，请重新修改");
        }
      },
    });
  }
</script>
</body>
</html>